using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CodeWork.DAL.BDHomePage;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_LinkEdit : SiteAdminBasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        Link link;

        if (Int32.TryParse(Request["id"], out id))
            link = Link.Load(id);
        else
            link = null;

        if (link != null)
        {
            if (!string.IsNullOrEmpty(link.LinkUrl))
				tbxLinkUrl.Text = link.LinkUrl;
			if (!string.IsNullOrEmpty(link.LinkDescription))
				tbxLinkDescription.Text = link.LinkDescription;
			tbxCurrentRating.Text = link.CurrentRating.ToString();
			tbxTotalRatingVote.Text = link.TotalRatingVote.ToString();
			tbxTotalVisits.Text = link.TotalVisits.ToString();
			tbxLinkCategoryId.Text = link.LinkCategoryId.ToString();
			cbxFlagged.Checked = link.Flagged;

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string linkUrl = Security.BindStringInput(tbxLinkUrl.Text, _securityLevel);
		string linkDescription = Security.BindStringInput(tbxLinkDescription.Text, _securityLevel);
		double currentRating = Security.BindDoubleInput(tbxCurrentRating.Text, _securityLevel);
		int totalRatingVote = Security.BindIntegerInput(tbxTotalRatingVote.Text, _securityLevel);
		int totalVisits = Security.BindIntegerInput(tbxTotalVisits.Text, _securityLevel);
		int linkCategoryId = Security.BindIntegerInput(tbxLinkCategoryId.Text, _securityLevel);
		bool flagged = cbxFlagged.Checked;

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (Link.Save(
				id, 
				linkUrl,
				linkDescription,
				currentRating,
				totalRatingVote,
				totalVisits,
				linkCategoryId,
				flagged
                ))
                    Response.Redirect("linklist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            Link link = Link.Create(
				linkUrl,
				linkDescription,
				currentRating,
				totalRatingVote,
				totalVisits,
				linkCategoryId,
				flagged
                );

            if (link != null)
                Response.Redirect("linklist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("linklist.aspx");
    }
}
